package io.mycat.eye.agent.task;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import io.mycat.eye.agent.bean.MycatServer;
import io.mycat.eye.agent.bean.MycatServerExample;
import io.mycat.eye.agent.bean.MycatSqlSum;
import io.mycat.eye.agent.dto.QueryResult;
import io.mycat.eye.agent.mapper.MycatServerMapper;
import io.mycat.eye.agent.mapper.MycatSqlSumMapper;

/**
 * Created by xqy on 18/4/29.
 */
@Component
public class MycatSqlSumTask extends AbstractTask {
    private static final Logger logger = LoggerFactory.getLogger(MycatSqlSumTask.class);
    private static final String SHOW_SQL = "show @@sql.sum";
    public static final long DELAY_TIME = 1 * 60 * 1000L;

    @Resource
    MycatServerMapper mycatServerMapper;

    @Resource
    MycatSqlSumMapper mapper;

    @Scheduled(fixedDelay = DELAY_TIME)
    public void execute() {
        logger.debug(SHOW_SQL + " task:" + DELAY_TIME + " ms");
        MycatServerExample mycatServerExample = new MycatServerExample();
        List<MycatServer> mycatServerList = mycatServerMapper.selectByExample(mycatServerExample);
        for (MycatServer server : mycatServerList) {
            logger.debug("server {}", server);
            String host = server.getServerHost();
            String port = server.getManagerPort().toString();
            String username = server.getUser();
            String password = server.getPassword();
            QueryResult<List<Map<Object, Object>>> statusQueryResult = getQueryResult(host, port, SHOW_SQL, username, password);

            if (!statusQueryResult.isSuccess()) {
                continue;
            }

            List<Map<Object, Object>> statusList = statusQueryResult.getData();
            logger.debug("{}", statusList.size());

            statusList.stream().forEach(o -> fillSql(o, server));
        }
    }
    
    private void fillSql(Map<Object, Object> o, MycatServer server) {
        MycatSqlSum sql = new MycatSqlSum();
        if(o.get("ID")!=null){
            sql.setId((long) o.get("ID"));
        }
        if(o.get("USER")!=null){
            sql.setcUser((String) o.get("USER"));
        }
        if(o.get("R")!=null){
            sql.setR((long) o.get("R"));
        }
        if(o.get("W")!=null){
            sql.setW((long) o.get("W"));
        }
        if(o.get("R%")!=null){
            sql.setrP(Float.valueOf((String) o.get("R%")));
        }
        if(o.get("MAX")!=null){
            sql.setSqlMax(Long.valueOf((String) o.get("MAX")));
        }
        if(o.get("NET_IN")!=null){
            sql.setNetIn((long) o.get("NET_IN"));
        }
        if(o.get("NET_OUT")!=null){
            sql.setNetOut((long) o.get("NET_OUT"));
        }
        if(o.get("TIME_COUNT")!=null){
            String timeCount = (String) o.get("TIME_COUNT");
            timeCount = timeCount.substring(1,timeCount.length() -1);
            sql.setTimeCount(timeCount);
        }
        if(o.get("TTL_COUNT")!=null){
            String ttlCount = (String) o.get("TTL_COUNT");
            ttlCount = ttlCount.substring(1,ttlCount.length()-1);
            sql.setTtlCount(ttlCount);
        }
        if(o.get("LAST_TIME")!=null){
            sql.setLastTime((long) o.get("LAST_TIME"));
        }
        sql.setCollectType("0");
        sql.setServerId(server.getId());
        sql.setCollectTime(System.currentTimeMillis());
        mapper.insertSelective(sql);
    }
}
